import pymysql

class DBUtil:
    # def __init__(self, host, user, password,database, port=3306):
    #     self.connect = pymysql.Connect(
    #                                 host=host,
    #                                 port=port,
    #                                 user=user,
    #                                 password=password,
    #                                 database=database,
    #                                 charset='utf8mb4',
    #                                 cursorclass=pymysql.cursors.DictCursor)  # 这个参数指的是数据库操作的结果是一个字典类型)
    def __init__(self, host, user, password, port=3306):
        self.connect = pymysql.Connect(
                                    host=host,
                                    port=port,
                                    user=user,
                                    password=password,
                                    charset='utf8mb4',
                                    cursorclass=pymysql.cursors.DictCursor)
    def select(self, sql):
        cursor = self.connect.cursor()  # 获取一个游标对象
        cursor.execute(sql)  # 执行sql语句
        # data = cursor.fetchall()  # 获取执行sql之后的所有结果
        data = cursor.fetchone() # 查询一条语句
        self.connect.commit()  # 提交事务
        cursor.close()
        return data


    def update(self, sql):
        """
        insert/update/delete
        :param sql:
        :return:
        """
        cursor = self.connect.cursor()
        cursor.execute(sql)
        self.connect.commit()
        cursor.close()

    def close(self):
        # 关闭数据库连接对象
        if self.connect:
            self.connect.close()

if __name__ == '__main__':
    dbutil = DBUtil(host="59.36.173.55",user="mtxshop_test",password="mtxshamo")
    # resp = dbutil.select(f"SELECT * FROM es_member_address WHERE member_id=89540 ORDER BY addr_id desc")
    resp = dbutil.select(f"select order_status from mtxshop_trade.es_order where sn =20240502000078")
    print(resp)